/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;


import cn.hutool.core.date.DateUtil;
import com.yami.shop.bean.param.*;
import com.yami.shop.common.util.Arith;
import com.yami.shop.dao.CustomerAnalysisMapper;
import com.yami.shop.service.CustomerAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2018/10/26.
 */
@Service
public class CustomerAnalysisServiceImpl implements CustomerAnalysisService {

    @Autowired
    private CustomerAnalysisMapper customerAnalysisMapper;

    @Override
    public MemberSurveyRespParam generalize(MemberReqParam memberReqParam) {
        MemberSurveyRespParam memberSurveyRespParam = new MemberSurveyRespParam();
        //统计比例数据
        Date startTime = null;
        //时间类型 1今日实时 2 近7天 3 近30天 4自然日 5自然月
        switch (memberReqParam.getDateType()){
            case 2:startTime = DateUtil.offsetDay(memberReqParam.getStartTime(),-7); break;
            case 3:startTime = DateUtil.offsetDay(memberReqParam.getStartTime(),-30); break;
            case 4:startTime = DateUtil.offsetDay(memberReqParam.getStartTime(),-30); break;
            default:startTime = DateUtil.offsetDay(memberReqParam.getStartTime(),-1);
        }
        List<MemberOverviewParam> customerRespParam = customerAnalysisMapper.generalize(startTime,memberReqParam);
        customerRespParam.sort(Comparator.comparingInt(MemberOverviewParam::getDataType));
        MemberOverviewParam memberOverviewParam = customerRespParam.get(0);
        getGeneralizeRate(memberOverviewParam,customerRespParam.get(1));
        memberSurveyRespParam.setMemberOverviewModel(memberOverviewParam);

        return memberSurveyRespParam;
    }
    private void getGeneralizeRate(MemberOverviewParam memberOverviewParam,MemberOverviewParam oldMemberOverviewParam){
        memberOverviewParam.setMemberSingleAmount(Arith.div(memberOverviewParam.getMemberPayAmount(),memberOverviewParam.getPayMember(),2));
        oldMemberOverviewParam.setMemberSingleAmount(Arith.div(oldMemberOverviewParam.getMemberPayAmount(),oldMemberOverviewParam.getPayMember(),2));
        memberOverviewParam.setTotalMemberRate(setRate(memberOverviewParam.getTotalMember().doubleValue(),oldMemberOverviewParam.getTotalMember().doubleValue()));
        memberOverviewParam.setNewMemberRate(setRate(memberOverviewParam.getNewMember().doubleValue(),oldMemberOverviewParam.getNewMember().doubleValue()));
        memberOverviewParam.setCouponMemberRate(setRate(memberOverviewParam.getCouponMember().doubleValue(),oldMemberOverviewParam.getCouponMember().doubleValue()));
        memberOverviewParam.setPayMemberRate(setRate(memberOverviewParam.getPayMember().doubleValue(),oldMemberOverviewParam.getPayMember().doubleValue()));
        memberOverviewParam.setMemberPayAmountRate(setRate(memberOverviewParam.getMemberPayAmount(),oldMemberOverviewParam.getMemberPayAmount()));
        memberOverviewParam.setMemberPayOrderRate(setRate(memberOverviewParam.getMemberPayOrder().doubleValue(),oldMemberOverviewParam.getMemberPayOrder().doubleValue()));
        memberOverviewParam.setMemberSingleAmountRate(setRate(memberOverviewParam.getMemberSingleAmount(),oldMemberOverviewParam.getMemberSingleAmount()));

    }

    private Double setRate(Double newNum,Double oldNum){
        if (newNum.equals(0.0) && oldNum.equals(0.0)){
            return 0.0;
        }else if (newNum > 0.0 && oldNum.equals(0.0)){
            return 0.0;
        }else if (newNum.equals(0.0) && oldNum > 0.0){
            return -100.0;
        }
        double sub = Arith.sub(newNum, oldNum);
        return Arith.mul(Arith.div(sub,oldNum,2),100);
    }
}
